package com.dean.utils;

import com.dean.exception.DeanRuntimeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.charset.Charset;
import java.util.Arrays;

/**
 * dean file 工具类
 */
public class DeanFileUtils {
    private static final Logger logger = LoggerFactory.getLogger(DeanFileUtils.class);
    /**
     * 按照字段长度换行
     * @param fromFile
     * @param splitCharLen
     * @param toFile
     */
    public static void splitByLen(String fromFile,int splitCharLen,String toFile){
        logger.info("{}.{}接受到参数:fromFile:[{}],splitCharLenL[{}],toFIle:[{}]",DeanFileUtils.class.getName(),"EBCDIC2ASCII",fromFile,splitCharLen,toFile);
        try(BufferedReader br = new BufferedReader(new FileReader(fromFile));
            BufferedWriter bw = new BufferedWriter(new FileWriter(toFile));){
            String content = br.readLine();
            int contentLen = content.length();
            logger.debug("contentLen:{}",contentLen);
            logger.debug("content:{}",content);
            int start = 0;
            int end = splitCharLen;
            int count = 1;
            while(start < contentLen){
                if(end > contentLen){
                    end = contentLen;
                }
                String row = content.substring(start,end);
                bw.write(row);
                bw.newLine();
                count++;
                start = end;
                end = count*splitCharLen;
            }
            bw.flush();
        } catch (FileNotFoundException e) {
            logger.error(e.getMessage(),e);
            throw DeanRuntimeException.throwException(996,e.getMessage());
        } catch (IOException e) {
            logger.error(e.getMessage(),e);
            throw DeanRuntimeException.throwException(996,e.getMessage());
        }


    }

    /**
     * EBCDIC2ASCII
     * @param fromFile
     * @param toFile
     */
    public static void EBCDIC2ASCII(String fromFile,String toFile) {
        logger.info("{}.{}接受到参数:fromFile:[{}],toFIle:[{}]",DeanFileUtils.class.getName(),"EBCDIC2ASCII",fromFile,toFile);
        try(InputStreamReader reader = new InputStreamReader(new FileInputStream(fromFile), Charset.forName("ibm500") );
            BufferedWriter bw = new BufferedWriter(new FileWriter(toFile));)
        {
            int charLen;
            StringBuilder sb = new StringBuilder();
            while((charLen = reader.read()) != -1){
                char theChar = (char) charLen;
                sb.append(theChar);
            }
            bw.write(sb.toString());
            bw.flush();
        } catch (FileNotFoundException e) {
            logger.error(e.getMessage(),e);
            throw DeanRuntimeException.throwException(996,e.getMessage());
        } catch (IOException e) {
            logger.error(e.getMessage(),e);
            throw DeanRuntimeException.throwException(996,e.getMessage());
        }

    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(args));
        String fromFile = args[1];
        String toFile = args[2];
        if("trans".equals(args[0])){
            EBCDIC2ASCII(fromFile,toFile);
        } else if ("split".equals(args[0])) {
            String splitCharLen = args[3];
            int charLen = Integer.parseInt(splitCharLen);
            splitByLen(fromFile,charLen,toFile);
        }else{
            System.out.println("非法操作,任务结束");
        }
    }
}
